package com.hzlj.position.locate.differentiation.common;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.fz.common.base.core.JsonUtils;
import com.google.common.collect.Lists;
import com.hzlj.common.message.common.dto.message.MessageNoticeResultDTO;
import com.hzlj.common.message.common.dto.message.MessageNoticeUserDTO;
import com.hzlj.position.config.common.enums.WarningType;
import com.hzlj.position.locate.common.dto.messageToday.MessageTodayAddDTO;
import com.hzlj.position.locate.common.dto.position.PositionKeyDTO;
import com.hzlj.position.locate.common.dto.position.PositionQueryResultDTO;
import com.hzlj.position.locate.common.model.Position;
import com.hzlj.position.locate.service.MessageTodayService;
import com.hzlj.position.locate.service.PositionService;
import com.hzlj.sqjz.jz.cache.JzJbxxCache;
import com.hzlj.sqjz.jz.common.dto.common.JzJbxxCacheDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Slf4j
@Service
public class PositionSysCheckNoticeGzry {
    @Resource
    private PositionService positionService;
    @Resource
    private MessageTodayService messageTodayService;
    @Resource
    private JzJbxxCache jzJbxxCache;

    /**
     * 同步发送核查结果信息
     */
    public Map<String, MessageNoticeResultDTO> notice(boolean normal,
                                                      PositionKeyDTO key,
                                                      String gzryRoles) {
        PositionQueryResultDTO position = positionService.getPosition(key.getId(), key.getPositionTime());
        String warningType = position.getWarningType();
        String messageTemplate = normal ?
                "locate-sys-check-normal-" + warningType + "-gzry"
                : "locate-sys-check-abn-" + warningType + "-gzry";
        MessageTodayAddDTO message = this.message(position, messageTemplate, gzryRoles);
        Map<String, MessageNoticeResultDTO> sendResult = messageTodayService.send(Lists.newArrayList(message));
        log.info("系统核验通知结果信息：sendResult={}", sendResult);
        return sendResult;
    }

    /**
     * 异步发送核查结果信息
     */
    public void noticeNoReport(Position position,
                               Date noticeTime,
                               String roles) {
        String messageTemplate = "locate-sys-check-no-report-" + position.getWarningType() + "-gzry";
        MessageTodayAddDTO message = this.message(
                BeanUtil.copyProperties(position, PositionQueryResultDTO.class),
                messageTemplate,
                roles
        );
        message.setNoticeTime(noticeTime);

        messageTodayService.sendAsync(Lists.newArrayList(message));
    }

    private MessageTodayAddDTO message(PositionQueryResultDTO position,
                                       String messageTemplate,
                                       String gzryRoles) {
        JzJbxxCacheDTO jzry = jzJbxxCache.getJzJbxxByJzId(position.getJzId());
        return new MessageTodayAddDTO()
                .setMessageTemplate(messageTemplate)
                .setServiceId(position.getId())
                .setBelongDay(DateUtil.beginOfDay(position.getPositionTime()))
                .setUser(MessageNoticeUserDTO.of(true, position.getJgCode(), gzryRoles))
                .setMessageParams(JsonUtils.obj2json(MapUtil.<String, Object>builder()
                        .put("jzryXm", position.getXm())
                        .put("jzrySjh", jzry.getSjh())
                        .put("warningTypeText", WarningType.of(position.getWarningType()).getText())
                        .put("positionTime", DateUtil.formatDateTime(position.getPositionTime()))
                        .build()
                ))
                .setCheckParams(JsonUtils.obj2json(MapUtil.<String, Object>builder()
                        .put("positionTime", DateUtil.formatDateTime(position.getPositionTime()))
                        .build()
                ));

    }


}
